package com.azure.database;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@MapperScan(basePackages = "com.azure.mapper.report", sqlSessionTemplateRef = "reportdbSqlSessionTemplate")
public class ReportdbMybatisConfig {

	@Bean(name = "reportdbConfig")
	@ConfigurationProperties(prefix = "mysql.datasource.reportdb")
	public BaseDBConfig dbConfig() {
		return new BaseDBConfig();
	}

	@Bean(name = "reportdbDataSource")
	public DataSource reportdbDataSource(@Qualifier("reportdbConfig") BaseDBConfig reportdbConfig) throws SQLException {
		System.out.println(reportdbConfig.getUrl());
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
		mysqlXaDataSource.setUrl(reportdbConfig.getUrl());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
		mysqlXaDataSource.setPassword(reportdbConfig.getPassword());
		mysqlXaDataSource.setUser(reportdbConfig.getUsername());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(mysqlXaDataSource);
		xaDataSource.setUniqueResourceName("reportdbDataSource");

		xaDataSource.setMinPoolSize(reportdbConfig.getMinPoolSize());
		xaDataSource.setMaxPoolSize(reportdbConfig.getMaxPoolSize());
		xaDataSource.setMaxLifetime(reportdbConfig.getMaxLifetime());
		xaDataSource.setBorrowConnectionTimeout(reportdbConfig.getBorrowConnectionTimeout());
		xaDataSource.setLoginTimeout(reportdbConfig.getLoginTimeout());
		xaDataSource.setMaintenanceInterval(reportdbConfig.getMaintenanceInterval());
		xaDataSource.setMaxIdleTime(reportdbConfig.getMaxIdleTime());
		xaDataSource.setTestQuery(reportdbConfig.getTestQuery());
		return xaDataSource;
	}

	@Bean(name = "reportdbSqlSessionFactory")
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("reportdbDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath:mapper/report/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "reportdbSqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("reportdbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
